﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    partial class FormControl : UserControl
    {
        private ClassView editionClass;
        private ClassData classData;
        private List<AttributeData> editionAttributes = new List<AttributeData>();
        private List<MethodData> editionMethods = new List<MethodData>();
        private List<RelationView> editionRelations = new List<RelationView>();
        private List<RelationView> referencedRelations = new List<RelationView>();

        public FormControl()
        {
            InitializeComponent();
            comboVisibility.DataSource = VisibilityType.GetTypes();
            comboModifiers.DataSource = ModifiersTypes.GetTypes();
        }

        private void buttonAddAtributes_Click(object sender, EventArgs e)
        {
            AttributesWindow attribute = new AttributesWindow();
            attribute.Show();
        }

        private void btnModifyAttributes_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                int index = listBoxAttributes.SelectedIndex;
                AttributesWindow addAttributes = new AttributesWindow(editionAttributes[index]);

                addAttributes.Show();
            }
        }

        private void btnAddMethods_Click(object sender, EventArgs e)
        {
            MethodsWindow anyadirMetodos = new MethodsWindow();
            anyadirMetodos.Show();
        }



        public void Refresh(ClassView classView)
        {
            editionClass = classView;
            classData = classView.GetClassData();

            txtClassName.Text =  classData.GetName();

            comboVisibility.SelectedItem = classData.GetVisibility();
            comboModifiers.SelectedItem = classData.GetModifier();

            listBoxAttributes.Items.Clear();
            foreach(AttributeData attr in classData.GetAttributes())
            {
                listBoxAttributes.Items.Add(attr.GetUMLRepresentation());
            }

            editionAttributes.Clear();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                editionAttributes.Add(classData.GetAttributes()[i]);
            }

            listBoxMethods.Items.Clear();
            foreach (MethodData mtd in classData.GetMethods())
            {
                listBoxMethods.Items.Add(mtd.GetUMLRepresentation());
            }

            editionMethods.Clear();
            for (int i = 0; i < classData.GetMethods().Count; i++)
            {
                editionMethods.Add(classData.GetMethods()[i]);
            }

            listBoxRelations.Items.Clear();
            editionRelations.Clear();
            referencedRelations.Clear();

            foreach (RelationView rel in WindowsManager.GetRelations())
            {
                if (rel.GetEndClass() == editionClass)
                {
                    editionRelations.Add(rel);
                    referencedRelations.Add(rel);
                    listBoxRelations.Items.Add(rel.GetIniClass().GetClassData().GetName() + " ("+rel.GetRelationType()+")");
                }
                else if (rel.GetIniClass() == editionClass)
                {
                    listBoxRelations.Items.Add(rel.GetEndClass().GetClassData().GetName() + " (" + rel.GetRelationType() + ")");
                    editionRelations.Add(rel);
                    referencedRelations.Add(rel);
                }
            }
           
        }
       
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (editionClass != null)
            {
                classData.SetName(txtClassName.Text);
                classData.SetAttributes(editionAttributes);
                classData.SetMethods(editionMethods);
                classData.SetModifier(comboModifiers.Text);
                classData.SetVisibility(comboVisibility.Text);

                editionClass.Refresh();

                //Edit the relations data
                //Delete the original relations
                foreach (RelationView rel in referencedRelations)
                {
                    WindowsManager.GetRelations().Remove(rel);
                }
                //Clear the referenced relations
                referencedRelations.Clear();

                //Add the modified relations
                foreach (RelationView rel in editionRelations)
                {
                    //Update the referenced relations with new values
                    referencedRelations.Add(rel);
                    WindowsManager.GetRelations().Add(rel);
                }
                WindowsManager.PaintRelations();
            }
        }

        public void Deselect()
        {
            txtClassName.Text = "";
            listBoxRelations.Items.Clear();
            listBoxMethods.Items.Clear();
            listBoxAttributes.Items.Clear();
            comboModifiers.SelectedItem = ModifiersTypes.NONE;
            comboVisibility.SelectedItem = VisibilityType.PUBLIC;

            editionClass = null;
        }
       
        public void AddAttribute(string name,string type,string visibility)
        {
            AttributeData attribute = new AttributeData(name,type,visibility);
            listBoxAttributes.Items.Add(attribute.GetUMLRepresentation());
            editionAttributes.Add(attribute);           
        }

        public void ModifyAttribute(string name, string type, string visibility)
        {
            int index = listBoxAttributes.SelectedIndex;
            AttributeData attr = new AttributeData(name, type, visibility);
            editionAttributes[index] = attr;
            listBoxAttributes.Items[index] = editionAttributes[index].GetUMLRepresentation();
        }

        private void btnDeleteAtrribute_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                for (int i = 0; i < editionAttributes.Count; i++)
                {
                
                    if (editionAttributes[i].GetUMLRepresentation() == listBoxAttributes.Text)
                    {
                        editionAttributes.Remove(editionAttributes[i]);
                    }
                }
              
                listBoxAttributes.Items.Remove(listBoxAttributes.SelectedItem);
            }
        }

        public void AddMethods(string visibilidad, string nombre, List<ParameterData> parametros, string retorno)
        {
            MethodData metodo = new MethodData(visibilidad, nombre, retorno);
            metodo.SetParameters(parametros);
            listBoxMethods.Items.Add(metodo.GetUMLRepresentation());
            editionMethods.Add(metodo);
        }

        public void EditMethod(string visibilidad, string nombre, List<ParameterData> parametros, string retorno)
        {
            int indice = listBoxMethods.SelectedIndex;
            MethodData metodo = new MethodData(visibilidad, nombre, retorno);
            metodo.SetParameters(parametros);
            editionMethods[indice] = metodo;

            listBoxMethods.Items[indice] = editionMethods[indice].GetUMLRepresentation();
        }

        private void btnDeleteMethod_Click(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                for (int i = 0; i < editionMethods.Count; i++)
                {
                    if (editionMethods[i].GetUMLRepresentation() == listBoxMethods.Text)
                    {
                        editionMethods.Remove(editionMethods[i]);
                    }
                }

                listBoxMethods.Items.Remove(listBoxMethods.SelectedItem);
            }
        }

        private void btnEditMethod_Click(object sender, EventArgs e)
        {
            if (listBoxMethods.SelectedItem != null)
            {
                int index = listBoxMethods.SelectedIndex;
                MethodsWindow editMethod = new MethodsWindow(editionMethods[index]);
                editMethod.Show();
            }
        }

        private void bttnEditRelations_Click(object sender, EventArgs e)
        {
            if (listBoxRelations.SelectedItem != null)
            {
                int index = listBoxRelations.SelectedIndex;
                if (!(editionRelations[index] is InheritanceView))
                {
                    RelationWindow editRelation = new RelationWindow(editionRelations[index]);
                    editRelation.Show();
                }
            }
        }

        public void EditRelation(string name, string iniCard, string endCard, string iniAttr, string endAttr, string iniAttrVisi, string endAttrVisi)
        {
            int index = listBoxRelations.SelectedIndex;

            RelationView origRelView = editionRelations[index];
            RelationView relView = origRelView.CreateObject();
            relView.SetIniClass(origRelView.GetIniClass());
            relView.SetEndClass(origRelView.GetEndClass());

            RelationData data = new RelationData();
            data.SetName(name);
            data.SetCardinalityIni(iniCard);
            data.SetCardinalityEnd(endCard);
            data.SetAttrNameIni(iniAttr);
            data.SetAttrNameEnd(endAttr);
            data.SetVisibilityIniAttribute(iniAttrVisi);
            data.SetVisibilityEndAttribute(endAttrVisi);
            relView.SetRelationData(data);

            editionRelations[index] = relView;
        }

        private void bttnDeleteRelation_Click(object sender, EventArgs e)
        {
            if (listBoxRelations.SelectedItem != null)
            {
                int index = listBoxRelations.SelectedIndex;
                editionRelations.RemoveAt(index);

                listBoxRelations.Items.Remove(listBoxRelations.SelectedItem);
            }
        }

        private void bttnDelete_Click(object sender, EventArgs e)
        {
            WindowsManager.DeleteClass();
        }

    }
}
